package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.ChapterVO;
import com.atguigu.eduservice.entity.vo.VideoVO;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exception.EduException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author zr
 * @since 2021-11-12
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public List<ChapterVO> getChapterVideoByCourseId(String courseId) {
        // 根据课程Id查询出对应的所有章节信息
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.orderByAsc("sort", "id");
        List<EduChapter> eduChapterLists = baseMapper.selectList(queryWrapper);

        // 根据课程Id查询出对应的所有小节信息
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id", courseId);
        videoWrapper.orderByAsc("sort", "id");
        List<EduVideo> eduVideoLists = eduVideoService.list(videoWrapper);

        // 准备用来封装章节信息的集合
        List<ChapterVO> chapterVOList = new ArrayList<>();

        // 遍历所有的章节集合
        for (int i = 0; i < eduChapterLists.size(); i++) {
            // 取出每个章节
            EduChapter eduChapter = eduChapterLists.get(i);
            // 创建空的章节对象
            ChapterVO chapterVO = new ChapterVO();
            // 使用工具类赋值数据
            BeanUtils.copyProperties(eduChapter, chapterVO);
            chapterVOList.add(chapterVO);

            // 创建用来封装小节的集合
            List<VideoVO> videoVOList = new ArrayList<>();

            for (int m = 0; m < eduVideoLists.size(); m++) {
                // 取出每个小节
                EduVideo eduVideo = eduVideoLists.get(m);
                // 进行判断
                if (Objects.equals(eduVideo.getChapterId(), eduChapter.getId())) {
                    // 创建空的集合
                    VideoVO videoVO = new VideoVO();
                    BeanUtils.copyProperties(eduVideo, videoVO);
                    videoVOList.add(videoVO);
                }
            }
            chapterVO.setChildren(videoVOList);
        }
        return chapterVOList;
    }

    /**
     * 根据章节Id删除
     *
     * @param chapterId
     * @return
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        // 先根据章节id 查询小节的记录  如果有小节 暂时不删除
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", chapterId);
        int count = eduVideoService.count(queryWrapper);
        // 进行判断 如果count大于0  不删除
        if (count > 0) {
            throw new EduException("不能删除", 20001);
        }
        int result = baseMapper.deleteById(chapterId);
        return result > 0;
    }

    /**
     * 根据课程Id删除章节
     *
     * @param courseId
     */
    @Override
    public void removeChapterById(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", queryWrapper);
        baseMapper.delete(queryWrapper);
    }
}
